﻿using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Reflection;
using XUCore.Extensions;
using XUCore.NetCore.DynamicWebApi;

namespace XUCore.NetCore.ApiTests
{
    public class ServiceActionRouteFactory : IActionRouteFactory
    {
        public string GetActionRoute(string areaName, string controllerName, ActionModel action)
        {
            var apiPreFix = action.GetApiPrefix();

            var serviceAttribute = action.ActionMethod.DeclaringType.GetCustomAttribute<ServiceAttribute>();

            var _controllerName = serviceAttribute.ServiceName.IsEmpty() ? controllerName : serviceAttribute.ServiceName;

            return $"{apiPreFix}/{_controllerName.RemovePostFix("Service")}/{action.ActionName.RemovePostFix(AppConsts.ActionPostfixes.ToArray())}";
        }
    }
}